<template>
  <div>
    <el-form ref="editFormRef" :rules="rules" :model="editForm.data" label-width="120">
      <el-form-item label="卡名称" prop="name">
        <el-input
          v-model="editForm.data.name"
          style="width: 320px"
          placeholder="请输入卡名称"
        />
      </el-form-item>
      <el-form-item label="单价（元）" prop="price">
        <el-input
          v-model="editForm.data.price"
          style="width: 320px"
          placeholder="请输入单价"
        />
      </el-form-item>
      <el-form-item label="单位" prop="type">
        <el-radio-group v-model="editForm.data.type">
          <el-radio v-for="item in typeList.data" :key="item.id" :value="item.id">
            {{ item.name }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(editFormRef)">取 消</el-button>
            <el-button class="confirm-btn" @click="submitForm(editFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getEditCparkCard } from "../../../card/typeCard/request.js";
const emits = defineEmits(["operationDone"]);

const props = defineProps({
  operationId: {
    type: Object,
    default: () => {},
  },
});
console.log(props.operationId);

const typeList = reactive({
  data: [
    {
      id: 1,
      name: "月",
    },
    {
      id: 2,
      name: "季",
    },
    {
      id: 3,
      name: "年",
    },
  ],
});

const allowOnlineList = reactive({
  data: [
    {
      id: 1,
      name: "允许",
    },
    {
      id: 0,
      name: "不允许",
    },
  ],
});

const editFormRef = ref("");
const editForm = reactive({
  data: {
    unit: 1,
    name: props.operationId.name,
    price: props.operationId.price,
    type: props.operationId.type,
  },
});

const rules = reactive({
  name: [{ required: true, message: "请输入卡名称", trigger: "blur" }],
  price: [{ required: true, message: "请输入单价", trigger: "blur" }],
  type: [{ required: true, message: "请选择单位", trigger: "change" }],
  scope: [{ required: true, message: "请选择应用范围", trigger: "change" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getEditCparkCard({
        id: props.operationId.id,
        data: editForm.data,
      }).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = async (formEl) => {
  if (!formEl) return;
  editForm.data = {
    unit: 1,
  };
  formEl.resetFields();
  emits("operationDone");
};
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
